const app = getApp()
import API from '../../api/api.js'
import Dialog from '../../lib/vant-weapp/dialog/dialog';
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    screenHeight: Number
  },

  /**
   * 组件的初始数据
   */
  data: {
    screenHeight: app.globalData.screenHeight,
    CustomBar: app.globalData.CustomBar,
    cardCur: 0,
    towerStart: 0,
    towerEnd: 0,
    swiperList: [],
  },

  ready: async function () {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    let userInfo = wx.getStorageSync('userInfo')
    this.setData({
      biYouList: userInfo.biYouLieBiao
    })
    await API.checkCountNumber('xinjian',userInfo.openid).then(res => {
      if (res.total > 4) {
        wx.showToast({
          title: '您的信箱空间已经不太多，请及时清理过期信件，邮箱空间不够将无法收到信件。',
          icon: "none",
          mask: true
        })
      }
    })
    await API.getXinJianByStatus(0).then(res => {
      if (res.length != 0) {
        this.data.swiperList = res
        this.towerSwiper('swiperList')
      }
      wx.hideLoading()
    })
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onDetailFunc: async function (e) {
      let item = e.currentTarget.dataset.value
      let index = e.currentTarget.dataset.index
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
      if (item.status == 0) {
        await API.updateXinJianStatus(item._id, 1).then(res=>{
          this.data.swiperList[index].status = 1
          this.towerSwiper('swiperList')
        })
      }
      wx.navigateTo({
        url: "/pages/xinjiandetail/xinjiandetail?id=" + item._id
      })
      wx.hideLoading()
    },

    //隐藏左侧弹出框
    onClose() {
      this.setData({
        show: false
      })
    },
    onWriteFunc: async function (e) {
      let item = e.currentTarget.dataset.value
      wx.setStorageSync('currPerson', item)
      await API.checkCountNumber('xinjian',item.openid).then(res => {
        if (res.total > 7) {
          wx.showToast({
            title: '他的邮箱已经没有空间，请稍后再向他发信。',
            icon: "none",
            mask: true
          })
          return
        }
        wx.navigateTo({
          url: "/pages/xiexing/xiexing?mode=send"
        })
      })
    },
    onDeleteBiYouFunc: function (e) {
      Dialog.confirm({
          title: '删除笔友',
          message: '请谨慎确定是否删除笔友，删除后不能再发送信件。',
        })
        .then(() => {
          wx.showLoading({
            title: '加载中...',
            mask: true
          })
          let userInfo = wx.getStorageSync('userInfo')
          let index = e.currentTarget.dataset.index
          userInfo.biYouLieBiao.splice(index, 1)
          this.setData({
            biYouList: userInfo.biYouLieBiao
          })
          API.updateBiYouList(userInfo._openid, userInfo.biYouLieBiao).then(res => {
            wx.hideLoading()
            wx.showToast({
              title: '删除笔友成功。',
              icon: "none"
            })
            wx.setStorageSync('userInfo', userInfo)
          })

        })
        .catch(() => {
          wx.showToast({
            title: '已取消删除笔友',
            icon: "none"
          })
        })

    },

    onDeleteXinJian: function (e) {
      let item = e.currentTarget.dataset.value
      let index = e.currentTarget.dataset.index
      Dialog.confirm({
          title: '删除信件',
          message: '请谨慎确定是否删除此信件，删除后无法恢复。',
        })
        .then(() => {
          wx.showLoading({
            title: '加载中...',
            mask: true
          })
          API.deleteCollectionItem("xinjian", item._id).then(res => {
            API.deleteImagesList(item.xinJianImagesList)
            this.data.swiperList.splice(index,1)
            this.towerSwiper('swiperList')
            wx.hideLoading()
            wx.showToast({
              title: '删除成功',
              icon: "none"
            })
          })
        })
        .catch(() => {
          wx.showToast({
            title: '已取消删除信件',
            icon: "none"
          })
        })
    },

    // 初始化towerSwiper
    towerSwiper(name) {
      let list = this.data[name];
      for (let i = 0; i < list.length; i++) {
        list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
        list[i].mLeft = i - parseInt(list.length / 2)
      }
      this.setData({
        swiperList: list
      })
    },
    // towerSwiper触摸开始
    towerStart(e) {
      this.setData({
        towerStart: e.touches[0].pageX
      })
    },
    // towerSwiper计算方向
    towerMove(e) {
      this.data.towerEnd = e.touches[0].pageX
      this.setData({
        direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
      })
    },
    // towerSwiper计算滚动
    towerEnd(e) {
      let direction = this.data.direction;
      let list = this.data.swiperList;
      if (this.data.towerEnd == 0) {
        this.data.towerEnd = 0
        return
      }
      if (direction == 'right') {
        let mLeft = list[0].mLeft;
        let zIndex = list[0].zIndex;
        for (let i = 1; i < list.length; i++) {
          list[i - 1].mLeft = list[i].mLeft
          list[i - 1].zIndex = list[i].zIndex
        }
        list[list.length - 1].mLeft = mLeft;
        list[list.length - 1].zIndex = zIndex;
        this.setData({
          swiperList: list
        })
        this.data.towerEnd = 0
      } else {
        let mLeft = list[list.length - 1].mLeft;
        let zIndex = list[list.length - 1].zIndex;
        for (let i = list.length - 1; i > 0; i--) {
          list[i].mLeft = list[i - 1].mLeft
          list[i].zIndex = list[i - 1].zIndex
        }
        list[0].mLeft = mLeft;
        list[0].zIndex = zIndex;
        this.setData({
          swiperList: list
        })
        this.data.towerEnd = 0
      }
    }
  }
})